<?php

namespace app\common\traits\controller;

use think\Config;
use think\Image;

/**
 * @method bool upload($fiele) 上传图片
 */
trait TraitUpload
{
    protected $water_image = 'water.png';
    protected $text = '@双辉旅程网';
    protected $uploadInfos = [];
    protected $_upload_image = '';
    protected $_error = '';
    protected $_upload_field = 'image';

    /**
     * 获取上传文件夹物理位置
     * @return string [description]
     */
    private function getUploadPath()
    {
        return ROOT_PATH . DS . 'public' . $this->getUploadurl();
    }
    /**
     * 生成文件名称
     * @return string [description]
     */
    private function buildSaveName()
    {
        return date('Ymd') . DS . md5(microtime(true));;
    }
    /**
     * 获取上传的文件夹目录
     * @return [type] [description]
     */
    private function getUploadurl()
    {
        return DS . 'uploads' . DS;
    }
    /**
     * 设置水印文字
     * @param string $text [description]
     */
    public function setText($text = '')
    {
        $this->text = $text ? $text : $this->text;
        return $this;
    }
    /**
     * 设置水印图片
     * @param string $water_image [description]
     */
    public function setWater($water_image = '')
    {
        $this->water_image = $water_image ? $water_image : $this->water_image;
        return $this;
    }
    public function mask()
    {
        if (!$this->_upload_image) {
            $this->setUploadField($this->_upload_field);
        }
        $this->_upload_image = !$this->_error ? $this->_upload_image->text($this->text, VENDOR_PATH . 'topthink/think-captcha/assets/zhttfs/1.ttf', 20, '#ffffff') : '';
        $this->_upload_image = !$this->_error ? $this->_upload_image->water($this->water_image, Image::WATER_SOUTHWEST, 50) : '';
        return $this;
    }
    /**
     * 小缩略图
     * @return [type] [description]
     */
    public function small()
    {
        return $this->thumb(400);
    }
    /**
     * 中等缩略图
     * @return [type] [description]
     */
    public function medium()
    {
        return $this->thumb(800);
    }
    public function thumb($width)
    {
        if (!$this->_upload_image) {
            $this->setUploadField($this->_upload_field);
        }
        $this->_upload_image = !$this->_error ? $this->_upload_image->thumb($width, $width / $this->_upload_image->width() * $this->_upload_image->height()) : '';
        return $this;
    }
    /**
     * 裁剪图片
     * @param  integer $w      裁剪区域宽度
     * @param  integer $h      裁剪区域高度
     * @param  integer $x      裁剪区域x坐标
     * @param  integer $y      裁剪区域y坐标
     * @return [type]    [description]
     */
    public function crop($w = null, $h = null, $x = 0, $y = 0)
    {
        if (!$this->_upload_image) {
            $this->setUploadField($this->_upload_field);
        }
        if ($w) {
            empty($h) && $h = $w;
            $this->_upload_image = !$this->_error ? $this->_upload_image->crop($w, $h, $x, $y) : '';
        }else{
            $this->_upload_image = !$this->_error ? $this->_upload_image : '';
        }
        return $this;
    }
    public function setUploadField($field)
    {
        if (!$field) {
            $this->_error = 'field is empty';
            return $this;
        }
        $this->_upload_field = $field;
        $pathname = $this->getUploadPath();
        $urlname = $this->getUploadurl();

        // 获取表单上传文件
        $file = request()->file($field);
        if (true !== $this->validate(['image' => $file], ['image' => 'require|image'])) {
            $this->_error = 'Illegal image file';
        } else {
            // 读取图片
            $this->_upload_image = Image::open($file);
        }
        return $this;
    }
    /**
     * 上传图片
     * @param  string $field [description]
     * @return bool 上传状态
     */
    public function upload($field = '')
    {
        $this->_upload_field = $field ? $field : $this->_upload_field;
        $pathname = $this->getUploadPath();
        $urlname = $this->getUploadurl();
        $savename = $this->buildSaveName();

        if (!$this->_upload_image) {
            $this->setUploadField($field);
        }
        if ($this->_error) {
            return false;
        }

        $this->uploadInfos['width'] = $this->_upload_image->width(); 
        // 返回图片的高度
        $this->uploadInfos['height'] = $this->_upload_image->height();
        $fileExtension = $this->uploadInfos['ext'] = $this->_upload_image->type();

        $savename = $this->buildSaveName();

        $filepath = $pathname . $savename . '.' . $fileExtension;

        if (!$this->checkPath(dirname($filepath))) {
            return false;
            //throw new \Exception("目录 {$path} 创建失败！", 1);
        }
        $this->uploadInfos['url'] = $urlname . $savename . '.' . $fileExtension;

        $this->_upload_image->save($filepath);
        return true;
    }
    /**
     * 上传成功后获取上传图片信息
     * @param  [type] $field [description]
     * @return [type]        [description]
     */
    public function getUploadInfos($field = null)
    {
        if ($field) {
            if (isset($this->uploadInfos[$field])) {
                return $this->uploadInfos[$field];
            }
            return ;
        }
        return $this->uploadInfos;
    }
    /**
     * 检查目录是否可写
     * @param  string   $path    目录
     * @return boolean
     */
    protected function checkPath($path)
    {
        if (is_dir($path)) {
            return true;
        }
        if (mkdir($path, 0755, true)) {
            return true;
        } else {
            return false;
        }
    }
}
